Skip to content

Stop enable_padding_free_metadata writing seq_lengths into the caller's examples - #8049

Open
vineethsaivs wants to merge 3 commits into
unslothai:mainfrom
vineethsaivs:fix/padding-free-metadata-no-input-mutation
Open

vineethsaivs wants to merge 3 commits into
unslothai:mainfrom
vineethsaivs:fix/padding-free-metadata-no-input-mutation

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

The padding-free collator wrapper adds seq_lengths to the caller's examples while deriving metadata. Pass copies containing the derived lengths to the wrapped collator, leaving the original examples untouched.

Rebased onto current main. The two CPU source-harness regressions pass for missing, explicit and null lengths, and changed-file pre-commit passes. Full package tests cannot import on this host and remain for upstream CI. The old CI logs have expired, so this refresh requests a fresh run.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

You are right, and the reason is bigger than a nullable column, so thanks for pushing on it. Fixed in 00450464b.

I had framed the write-back as caching, which was wrong. It was load-bearing. TRL's DataCollatorForLanguageModeling decides whether to use seq_lengths at all from the first row (sft_trainer.py line 464: [example["seq_lengths"] for example in examples] if "seq_lengths" in examples[0] else None) and only then reads it off every row, so removing the key changes what it builds.

Two things break, not one. Your null case reaches sum(None). The one I had not seen is a batch mixing an unpacked row with a packed one, where the key is missing on row 0 so TRL ignores seq_lengths entirely and falls back to torch.arange:

with derived lengths : [0, 1, 2, 0, 1, 0, 1]
key missing on row 0 : [0, 1, 2, 0, 1, 2, 3]

The packed row's second document starts at position 2 instead of 0, so it attends across the document boundary. That is silent, and it is exactly what padding-free metadata exists to prevent.

Your suggested shape is what I did: pass a normalized copy to the wrapped collator, built only when something was actually derived, so the common path still hands the caller's own list straight through with no per-row copies. Ran it three ways against upstream/main, this branch before the change and after: main passes everything except leaving seq_lengths on the caller's rows, the previous commit fixes that and breaks the null case and the mixed batch, and the new commit passes all five. The test now asserts what reaches the collator rather than only what comes back.

@vineethsaivs
vineethsaivs force-pushed the fix/padding-free-metadata-no-input-mutation branch from 30d8d4f to ed30fc6 Compare September 16, 2026 19:44
…'s examples

Put the derived lengths on a shallow copy, so the wrapped collator still sees them
without the caller's own rows being mutated.
@vineethsaivs
vineethsaivs force-pushed the fix/padding-free-metadata-no-input-mutation branch from 0c1e631 to 841d32f Compare September 18, 2026 20:07
@danielhanchen danielhanchen self-assigned this Oct 1, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #8049, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#309.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 7cca354cbe (this PR's a6b907f34)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 7cca354cbe

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 7cca354cbe (this PR's a6b907f34).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants